package flow.action;

import flow.model.FlowDefine;
import flow.model.FlowSection;
import flow.service.FlowDefineService;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.ButtonType;
import m.html.element.other.ItemsQuery;
import m.html.element.other.Param;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.action.ManageAction;

@ActionMeta(name="flow/flowDefine")
public class FlowDefineAction extends ManageAction {
	private String oper;
	private FlowDefine model;

	public JSONMessage getDefine() {
		JSONMessage result=new JSONMessage();
		try {
			result=getService(FlowDefineService.class).getDefine(model);
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			String msg=getService(FlowDefineService.class).save(model);
			result.push("model", model);
			fillJSONResult(result,true,msg);
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doIssue(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			getService(FlowDefineService.class).issue(model);
			result.push("model.oid", model.getOid());
			result.push("model.issueStatus", model.getIssueStatus());
			fillJSONResult(result,true,"发布成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doDelete(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			getService(FlowDefineService.class).delete(model);
			fillJSONResult(result,true,"删除成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}

	@RequestMeta(params= {"oid"})
	public JSONMessage getAllSection(String oid) {
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			QueryCondition cond=QueryCondition.eq("flowDefine.oid", oid);
			result.push("sectionList", new ItemsQuery(FlowSection.class,"name","oid").setTitleExpression("concat(name,' (',identity,')')").addCond(cond).toDatas());
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public VueResult toEdit() throws Exception {
		verifyMenu("A3101");
		verifyAdminOperPower("manage_flow_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())){
			model=ModelQueryList.getModel(model,"*");
		}else if(null!=model&&!StringUtil.isSpace(model.getIdentity())){
			ModelCheckUtil.checkUniqueCombine(model, new String[]{"identity"},"标识已存在");
			model.setIssueStatus("C");
			model.setVersion(1);
		}else {
			throw new MException(this.getClass(), "请输入标识");
		}
		return new VuePageResult("page/base/flow/editFlowDefine.js")
			.addParam("model",model)
			.addParam("oper", oper);
	}
	public VueResult toNewEdit() throws Exception {
		verifyMenu("A3101");
		model=getService(FlowDefineService.class).saveNew(model);
		return toEdit();
	}
	public VueResult flowDefineList() throws Exception{
		verifyMenu("A3101");
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程定义列表",FlowDefine.class,"action/flow/flowDefine/flowDefineList");
		result.setSubTitle("添加或者编辑流程信息。");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("标识", "identity",true));
		result.addCol(toLinkColumn("名称", "name",true,
			new ListEvent("action/flow/flowDefine/toEdit", EventType.MODAL,900).addParam("model.oid","oid")
		));
		result.addCol(toTextColumn("描述", "description",200,true));
		result.addCol(toTextColumn("发布状态", "issueStatus",true).setDictOption(FlowDefine.class,"issueStatus"));
		result.addCol(toDateColumn("发布时间", "issueDate",true).setSort(true));
		result.addCol(toIntegerColumn("版本", "version").setCount(ListCount.NONE).setSort(true));
		if(power) result.addCol(toSpaceColumn("操作",150,
			ListElement.test("record.issueStatus=='C'", 
				ListButton.modal("修改", ButtonType.PRIMARY,"action/flow/flowDefine/toEdit",900, SuccessMethod.REFRESH)
					.addParam("model.oid","oid").addQueryParam(Param.value("oper", "edit"))
			),
			ListElement.elseTest("record.issueStatus=='Y'",
					ListButton.modal("生成新版本",ButtonType.PRIMARY,"action/flow/flowDefine/toNewEdit",900,SuccessMethod.MUST_REFRESH)
					.addParam("model.oid","oid").addQueryParam(Param.value("oper", "edit")).setConfirm("确定要生成该流程的新版本吗?")
			),
			ListElement.test("record.issueStatus=='C'", 
				ListButton.ajax("删除",ButtonType.ERROR,"action/flow/flowDefine/doDelete",SuccessMethod.REFRESH)
					.addParam("model.oid","oid").setConfirm("确定要删除该流程吗?")
			)
		));
		if(power) result.addButton(
			ListButton.modal("新增",ButtonType.PRIMARY,"page/base/flow/createFlowDefine.js",400,SuccessMethod.REFRESH)
		);
		//result.addCondition(QueryCondition.in("issueStatus", "C","Y"));
		return result;
	}
	public FlowDefine getModel() {
		return model;
	}

	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}
	public void setModel(FlowDefine model) {
		this.model = model;
	}
	
}
